﻿using System;
using System.Windows;
using System.Windows.Controls;
using CoreSharp.Keyboard.Layouts;
using Microsoft.Phone.Controls;

namespace CoreSharp.Controls
{
	public partial class Keyboard : UserControl
	{
        public event Action<string> KeyInput;
        public event EventHandler RequestEnKeyboard;
        public event EventHandler KeyDelete;

        public KeyboardLayout CurrentLayout
        {
            get
            {
                return this.NormalLayout.DataContext as KeyboardLayout;
            }
        }

        private void OnKeyPressed(string content)
        {
            if (content == null)
                return;

            if (this.KeyInput == null)
                return;

            this.KeyInput(content);
            //this.PressBump.Begin();
        }

        private void OnKeyDelete()
        {
            if (this.KeyDelete != null)
                this.KeyDelete(this, EventArgs.Empty);
        }

        private void OnRequestEnKeyboard()
        {
            if (this.RequestEnKeyboard != null)
                this.RequestEnKeyboard(this, EventArgs.Empty);
        }

		public Keyboard()
		{
			// Required to initialize variables
			InitializeComponent();

            this.NormalLayout.KeyInput += delegate(string input)
            {
                this.OnKeyPressed(input);
            };
            this.ShiftLayout.KeyInput += delegate(string input)
            {
                this.OnKeyPressed(input);
                this.ToggleShift();
            };
            this.AltLayout.KeyInput += delegate(string input)
            {
                this.OnKeyPressed(input);
                this.ToggleAlt();
            };

            this.NormalLayout.DataContext = ThaiKeyboardLayout.Default;
            this.ShiftLayout.DataContext = ThaiShiftKeyboardLayout.Default;
            this.AltLayout.DataContext = ThaiVowelConsonantKeyboardLayout.Default;
            //VisualStateManager.GoToState(this, "Load", false);

//#if DEBUG
//            if (!DesignerProperties.GetIsInDesignMode(this))
//            {
//#endif
//                ThreadPool.QueueUserWorkItem(delegate
//                {
//                    Thread.Sleep(100);
//                    var shift = ThaiShiftKeyboardLayout.Default;

//                    this.Dispatcher.BeginInvoke(delegate
//                    {
//                        this.ShiftLayout.DataContext = shift;
//                    });

//                    Thread.Sleep(100);
//                    var alt = ThaiVowelConsonantKeyboardLayout.Default;
//                    this.Dispatcher.BeginInvoke(delegate
//                    {
//                        this.AltLayout.DataContext = alt;
//                        VisualStateManager.GoToState(this, "NotLoad", true);
//                    });
//                });
//#if DEBUG
//            }
//#endif
		}

        private bool _IsShifted = false;
        public void ToggleShift()
        {
            if (_IsShifted)
            {
                VisualStateManager.GoToState(this, "NormalKeys", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "ShiftKeys", true);
            }
            _IsShifted = !_IsShifted;
            _IsAlt = false;
        }

        private bool _IsAlt = false;
        public void ToggleAlt()
        {
            if (_IsAlt)
            {
                VisualStateManager.GoToState(this, "NormalKeys", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "AltKeys", true);
            }
            _IsAlt = !_IsAlt;
            _IsShifted = false;
        }

        private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
        {
            if (e.Direction == Orientation.Vertical)
            {
                if (e.VerticalVelocity < 0) // up
                {
                    if (_IsAlt)
                    {
                        this.ToggleAlt();
                    }
                    else
                    {
                        this.ToggleShift();
                    }
                }
                else
                {
                    if (_IsShifted)
                    {
                        this.ToggleShift();
                    }
                    else
                    {
                        this.ToggleAlt();
                    }
                }
            }
            else
            {
                if (e.HorizontalVelocity < 0) // left
                {
                    this.OnKeyDelete();
                    this.DelTilt.Begin();
                }
                else
                {
                    this.OnKeyPressed(" ");
                    this.SpaceTilt.Begin();
                }
            }

            

            e.Handled = true;
        }
	}
}